#include <map>
#include<iostream>
using namespace std;

int* swapson(int* a, std::map<int, int>& b, int* result, int L1, int R1, int L2, int R2) {
    if (L1 > R1)
        return result;
    *result++ = a[L1];
    int m = b[a[L1++]];
    result = swapson(a, b, result, R1 - (R2 - m) + 1, R1, m + 1, R2);
    result = swapson(a, b, result, L1, L1 + (m - L2) - 1, L2, m - 1);
    return result;
}

std::map<int, int> pos;
void solve(int n, int* preOrder, int* inOrder, int* outOrder) {
    for (int i = 0; i < n; i++)
        pos[inOrder[i]] = i;
    swapson(preOrder, pos, outOrder, 0, n - 1, 0, n - 1);
}